package com.snj.bestdealer.main;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.snj.bestdealer.R;
import com.snj.bestdealer.lib.CustomHttpClient;
import com.snj.bestdealer.lib.Global;
import com.snj.bestdealer.request.MyRequestDetailActivity;
import com.snj.bestdealer.request.RequestActivity;
import com.snj.bestdealer.request.RequestAdapter;
import com.snj.bestdealer.request.RequestItem;

public class RequestFragment extends Fragment implements OnClickListener {
	private final String TAG = "RequestFragment";	
	
	
	
	Button btn_newrequest = null;
	ListView lstv_myreq = null;
	
	
	ArrayList<RequestItem> mReqList = new ArrayList<RequestItem>();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = inflater.inflate(R.layout.fragment_main_request,
				container, false);
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(TAG, "onActivityCreated");
		
		super.onActivityCreated(savedInstanceState);
		init_widget();
	}
	
	private void init_widget() {
		final View view = getView();
		
		btn_newrequest = (Button)view.findViewById(R.id.btn_newrequest);
		btn_newrequest.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent reqintent = new Intent(view.getContext(), RequestActivity.class);
				startActivity(reqintent);
			}
		});
		
		lstv_myreq = (ListView)view.findViewById(R.id.lstv_myreq);
		lstv_myreq.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				
				RequestAdapter adapter = (RequestAdapter)lstv_myreq.getAdapter();
				
				Intent detailintent = new Intent(getActivity(), MyRequestDetailActivity.class);
				detailintent.putExtra("reqno", adapter.getItem(position).GetNo());
				detailintent.putExtra("brand", adapter.getItem(position).GetBrandID());
				detailintent.putExtra("vehicle", adapter.getItem(position).GetVehicle());
				detailintent.putExtra("location", adapter.getItem(position).GetLocation());
				detailintent.putExtra("paytype", adapter.getItem(position).GetPayType());
				detailintent.putExtra("reqdate", adapter.getItem(position).GetRequstDate());
				detailintent.putExtra("etc", adapter.getItem(position).GetEtc());
				
				startActivity(detailintent);
				
			}
		});
		
	}
	
		
	@Override
	public void onResume() {
		super.onResume();
		
		new AsyncTask<String, String, String>() {

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("appkey", "1234"));
				postParameters.add(new BasicNameValuePair("usrtype", Global.usrtype));
				postParameters.add(new BasicNameValuePair("consumer", Global.usremail));
				postParameters.add(new BasicNameValuePair("devicekey", Global.devicekey));
				
				String result = "";
				JSONObject resultobj = null;

				try {
					result = CustomHttpClient.executeHttpPost(
							Global.requsetlisturl, postParameters);
					Log.d(TAG, result);
					resultobj = new JSONObject(result);
					
					if(resultobj.getString("result").equals("success"))
					{
						Bitmap logo = null;
						
						JSONArray arr = resultobj.getJSONArray("data");
						mReqList.clear();
						for(int i=0; i<arr.length(); i++)
						{
							JSONObject obj = arr.getJSONObject(i);
							
							if(Global.logo_map.get(obj.getString("brandid")) == null)
								logo = BitmapFactory.decodeResource(getResources(), R.drawable.etc);
							else
								
								logo = BitmapFactory.decodeResource(getResources(), Global.logo_map.get(obj.getString("brandid")));
							mReqList.add(new RequestItem(obj.getString("no"), logo, obj.getString("paytype"), obj.getString("reqdate"), obj.getString("location"), obj.getString("consumername"), obj.getString("brand"), obj.getString("brandid"), obj.getString("vehicle"), obj.getString("option"), obj.getString("etc"), obj.getString("isopenphone").equals("1") ? true : false, obj.getString("isopenemail").equals("1") ? true : false, obj.getString("reqterm"), obj.getString("reqcnt"), getResources()));
						}
						
						return "success";
					}
					
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				
				return null;
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				if(result == null) return;
				
				if(result.equals("success"))
				{
					RequestAdapter adapter = new RequestAdapter(getActivity(), R.layout.list_request, mReqList);
					lstv_myreq.setAdapter(adapter);
				}
			}
			
		}.execute(Global.usremail);
	}

	@Override
	public void onClick(View v) {
	}
}
